package com.crwal.app.config;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import com.crwal.app.bean.CrwalUrl;
import com.crwal.app.bean.News;
import com.crwal.app.service.WebCrwalService;
import com.crwal.app.utils.ExcelUtils;

import lombok.extern.slf4j.Slf4j;
 
@Component
@Slf4j
public class MyCrwalAsync {
	
	@Autowired
	private WebCrwalService service;
	
    @Async("taskExecutor")  //使用taskExecutor线程池
    public String natureworksllc(String companyName, String companyHref, String fileName, String maxDate) {
    	List<News> newsList = new ArrayList<News>();
        try {
        	newsList = service.natureworksllc(companyName, companyHref, maxDate);
        	
        	if(null != newsList && newsList.size() > 0) {
				ExcelUtils.exportCrwalResult(CrwalUrl.getTempLocDirectory(), fileName, newsList);
				log.debug("导出["+companyName+"]文件数据成功");
			} else {
				log.debug("导出文件，["+companyName+"]没有抓取到数据。。。");
			}
        } catch (Exception e) {
        	log.error("抓取["+companyName+"]数据异常！",e);
        }
        return null;
    }
    
    @Async("taskExecutor")  //使用taskExecutor线程池
    public String corbion(String companyName, String companyHref, String fileName, String maxDate) {
    	List<News> newsList = new ArrayList<News>();
        try {
        	newsList = service.corbion(companyName, companyHref, maxDate);
        	
        	if(null != newsList && newsList.size() > 0) {
				ExcelUtils.exportCrwalResult(CrwalUrl.getTempLocDirectory(), fileName, newsList);
				log.debug("导出["+companyName+"]文件数据成功");
			} else {
				log.debug("导出文件，["+companyName+"]没有抓取到数据。。。");
			}
        } catch (Exception e) {
        	log.error("抓取["+companyName+"]数据异常！",e);
        }
        return null;
    }
    
    @Async("taskExecutor")  //使用taskExecutor线程池
    public String totalcorbion(String companyName, String companyHref, String fileName, String maxDate) {
    	List<News> newsList = new ArrayList<News>();
        try {
        	newsList = service.totalcorbion(companyName, companyHref, maxDate);
        	if(null != newsList && newsList.size() > 0) {
				ExcelUtils.exportCrwalResult(CrwalUrl.getTempLocDirectory(), fileName, newsList);
				log.debug("导出["+companyName+"]文件数据成功");
			} else {
				log.debug("导出文件，["+companyName+"]没有抓取到数据。。。");
			}
        } catch (Exception e) {
        	log.error("抓取["+companyName+"]数据异常！",e);
        }
        return null;
    }
    
    @Async("taskExecutor")  //使用taskExecutor线程池
    public String pttmcc(String companyName, String companyHref, String fileName, String maxDate) {
    	List<News> newsList = new ArrayList<News>();
        try {
        	newsList = service.pttmcc(companyName, companyHref, maxDate);
        	if(null != newsList && newsList.size() > 0) {
				ExcelUtils.exportCrwalResult(CrwalUrl.getTempLocDirectory(), fileName, newsList);
				log.debug("导出["+companyName+"]文件数据成功");
			} else {
				log.debug("导出文件，["+companyName+"]没有抓取到数据。。。");
			}
        } catch (Exception e) {
        	log.error("抓取["+companyName+"]数据异常！",e);
        }
        return null;
    }

    @Async("taskExecutor")  //使用taskExecutor线程池
    public String cjbio(String companyName, String companyHref, String fileName, String maxDate) {
    	List<News> newsList = new ArrayList<News>();
        try {
        	newsList = service.cjbio(companyName, companyHref, maxDate);
        	if(null != newsList && newsList.size() > 0) {
				ExcelUtils.exportCrwalResult(CrwalUrl.getTempLocDirectory(), fileName, newsList);
				log.debug("导出["+companyName+"]文件数据成功");
			} else {
				log.debug("导出文件，["+companyName+"]没有抓取到数据。。。");
			}
        } catch (Exception e) {
        	log.error("抓取["+companyName+"]数据异常！",e);
        }
        return null;
    }
    
    @Async("taskExecutor")  //使用taskExecutor线程池
    public String basf(String companyName, String companyHref, String fileName, String maxDate) {
    	List<News> newsList = new ArrayList<News>();
        try {
        	newsList = service.basf(companyName, companyHref, maxDate);
        	if(null != newsList && newsList.size() > 0) {
				ExcelUtils.exportCrwalResult(CrwalUrl.getTempLocDirectory(), fileName, newsList);
				log.debug("导出["+companyName+"]文件数据成功");
			} else {
				log.debug("导出文件，["+companyName+"]没有抓取到数据。。。");
			}
        } catch (Exception e) {
        	log.error("抓取["+companyName+"]数据异常！",e);
        }
        return null;
    }
    
    @Async("taskExecutor")  //使用taskExecutor线程池
    public String arkema(String companyName, String companyHref, String fileName, String maxDate) {
    	List<News> newsList = new ArrayList<News>();
        try {
        	newsList = service.arkema(companyName, companyHref, maxDate);
        	if(null != newsList && newsList.size() > 0) {
				ExcelUtils.exportCrwalResult(CrwalUrl.getTempLocDirectory(), fileName, newsList);
				log.debug("导出["+companyName+"]文件数据成功");
			} else {
				log.debug("导出文件，["+companyName+"]没有抓取到数据。。。");
			}
        } catch (Exception e) {
        	log.error("抓取["+companyName+"]数据异常！",e);
        }
        return null;
    }
    
    @Async("taskExecutor")  //使用taskExecutor线程池
    public String plasticker(String companyName, String companyHref, String fileName, String maxDate) {
    	List<News> newsList = new ArrayList<News>();
        try {
        	newsList = service.plasticker(companyName, companyHref, maxDate);
        	if(null != newsList && newsList.size() > 0) {
				ExcelUtils.exportCrwalResult(CrwalUrl.getTempLocDirectory(), fileName, newsList);
				log.debug("导出["+companyName+"]文件数据成功");
			} else {
				log.debug("导出文件，["+companyName+"]没有抓取到数据。。。");
			}
        } catch (Exception e) {
        	log.error("抓取["+companyName+"]数据异常！",e);
        }
        return null;
    }
    
    @Async("taskExecutor")  //使用taskExecutor线程池
    public String bioplasticsnews(String companyName, String companyHref, String fileName, String maxDate) {
    	List<News> newsList = new ArrayList<News>();
        try {
        	newsList = service.bioplasticsnews(companyName, companyHref, maxDate);
        	if(null != newsList && newsList.size() > 0) {
				ExcelUtils.exportCrwalResult(CrwalUrl.getTempLocDirectory(), fileName, newsList);
				log.debug("导出["+companyName+"]文件数据成功");
			} else {
				log.debug("导出文件，["+companyName+"]没有抓取到数据。。。");
			}
        } catch (Exception e) {
        	log.error("抓取["+companyName+"]数据异常！",e);
        }
        return null;
    }
    
    @Async("taskExecutor")  //使用taskExecutor线程池
    public String bioplasticsmagazine(String companyName, String companyHref, String fileName, String maxDate) {
    	List<News> newsList = new ArrayList<News>();
        try {
        	newsList = service.bioplasticsmagazine(companyName, companyHref, maxDate);
        	if(null != newsList && newsList.size() > 0) {
				ExcelUtils.exportCrwalResult(CrwalUrl.getTempLocDirectory(), fileName, newsList);
				log.debug("导出["+companyName+"]文件数据成功");
			} else {
				log.debug("导出文件，["+companyName+"]没有抓取到数据。。。");
			}
        } catch (Exception e) {
        	log.error("抓取["+companyName+"]数据异常！",e);
        }
        return null;
    }
    
    
    @Async("taskExecutor")  //使用taskExecutor线程池
    public String plasticsnews(String companyName, String companyHref, String fileName, String maxDate) {
    	List<News> newsList = new ArrayList<News>();
        try {
        	newsList = service.plasticsnews(companyName, companyHref, maxDate);
        	
        	if(null != newsList && newsList.size() > 0) {
				ExcelUtils.exportCrwalResult(CrwalUrl.getTempLocDirectory(), fileName, newsList);
				log.debug("导出["+companyName+"]文件数据成功");
			} else {
				log.debug("导出文件，["+companyName+"]没有抓取到数据。。。");
			}
        } catch (Exception e) {
        	log.error("抓取["+companyName+"]数据异常！",e);
        }
        return null;
    }
    
    @Async("taskExecutor")  //使用taskExecutor线程池
    public String biobased(String companyName, String companyHref, String fileName, String maxDate) {
    	List<News> newsList = new ArrayList<News>();
        try {
        	newsList = service.biobased(companyName, companyHref, maxDate);
        	if(null != newsList && newsList.size() > 0) {
				ExcelUtils.exportCrwalResult(CrwalUrl.getTempLocDirectory(), fileName, newsList);
				log.debug("导出["+companyName+"]文件数据成功");
			} else {
				log.debug("导出文件，["+companyName+"]没有抓取到数据。。。");
			}
        } catch (Exception e) {
        	log.error("抓取["+companyName+"]数据异常！",e);
        }
        return null;
    }
    
    
}